﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace RL
{
    /// <summary>
    /// Логика взаимодействия для ucDayWeeks.xaml
    /// </summary>
    public partial class ucDayWeeks : UserControl
    {
        private routelistEntities db;
        //private routelistEntities db;
        private int _id;

        public int ScheduleID
        {
            get
            {
                SaveScheduleData();
                return (int)GetValue(ScheduleProperty); }
            set
            {
                SetValue(ScheduleProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for Schedule.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ScheduleProperty =
            DependencyProperty.Register("ScheduleID", typeof(int), typeof(ucDayWeeks), new UIPropertyMetadata(0, new PropertyChangedCallback(propchanged)));

        private static void propchanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
            ucDayWeeks sh = (ucDayWeeks) d;
            sh._id = (int)e.NewValue;
            sh.LoadScheduleData(sh._id);
}
        //public int ScheduleID {
        //    get
        //    {
        //        _id = SaveScheduleData();
        //        return _id;
        //    }
        //    set 
        //    { 
        //        _id = value;
        //        LoadScheduleData(_id);
        //    }
        //}

        public ucDayWeeks()
        {
            InitializeComponent();
            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                db = new routelistEntities(Properties.Settings.Default.DebugConnection);
            }
            else
            {
                db = new routelistEntities();
            }
        }

        private void LoadScheduleData(int id)
        {
            //if (-1 == id)
            //{
            //    checkBoxMO.IsChecked = false;
            //    checkBoxTU.IsChecked = false;
            //    checkBoxWE.IsChecked = false;
            //    checkBoxTH.IsChecked = false;
            //    checkBoxFR.IsChecked = false;
            //    checkBoxSA.IsChecked = false;
            //    checkBoxSU.IsChecked = false;
            //    return;
            //}
            Schedule dt = (from o in db.Schedule where o.id == id select o).FirstOrDefault();
            checkBoxMO.IsChecked = (dt == null) ? false : dt.monday;
            checkBoxTU.IsChecked = (dt == null) ? false : dt.tuesday;
            checkBoxWE.IsChecked = (dt == null) ? false : dt.wednesday;
            checkBoxTH.IsChecked = (dt == null) ? false : dt.thursday;
            checkBoxFR.IsChecked = (dt == null) ? false : dt.friday;
            checkBoxSA.IsChecked = (dt == null) ? false : dt.saturday;
            checkBoxSU.IsChecked = (dt == null) ? false : dt.sunday;
        }

        private int SaveScheduleData()
        {
            int result = 0;
            Schedule dt = (from o in db.Schedule
                           where o.monday == checkBoxMO.IsChecked &&
                               o.tuesday == checkBoxTU.IsChecked &&
                               o.wednesday == checkBoxWE.IsChecked &&
                               o.thursday == checkBoxTH.IsChecked &&
                               o.friday == checkBoxFR.IsChecked &&
                               o.saturday == checkBoxSA.IsChecked &&
                               o.sunday == checkBoxSU.IsChecked
                           select o).FirstOrDefault();
            if (null == dt)
            {
                dt = new Schedule()
                {
                    monday = checkBoxMO.IsChecked ?? false,
                    tuesday = checkBoxTU.IsChecked ?? false,
                    wednesday = checkBoxWE.IsChecked ?? false,
                    thursday = checkBoxTH.IsChecked ?? false,
                    friday = checkBoxFR.IsChecked ?? false,
                    saturday = checkBoxSA.IsChecked ?? false,
                    sunday = checkBoxSU.IsChecked ?? false
                };
                db.Schedule.AddObject(dt);
                try
                {
                    db.SaveChanges();
                    result = dt.id;
                }
                catch (Exception)
                {
                    result = -1;
                }
            }
            else
            {
                result = dt.id;
            }
            SetValue(ScheduleProperty, result);
            return result;
        }

        private void UserControl_LostFocus(object sender, RoutedEventArgs e)
        {
            SaveScheduleData();
        }
    }
}
